package com.py.framework.controller;

import com.alibaba.fastjson.JSONObject;
import com.py.framework.common.controller.SystemController;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.entity.HolidayEntity;
import com.py.framework.service.HolidayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 节假日控制器
 *
 * @author YaoS
 * @version shanghai
 * @date 2019-02-20 16:37
 */
@RestController
@RequestMapping(value = "/holiday")
public class HolidayController extends SystemController {

    public static Integer CONVENTIONAL = -1;
    public static Integer HOLI_DAY = 0;
    public static Integer WORK_DAY = 1;

    @Autowired
    private HolidayService holidayService;

    /**
     * 功能描述: 添加节假日
     *
     * @param list 节假日列表
     * @return RestServiceResult 添加结果
     * @author YaoS
     * @date 19/2/21 10:23
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RestServiceResult<Object> addHoliday(@RequestParam String list) {
        List<HolidayEntity> entityList = JSONObject.parseArray(list, HolidayEntity.class);
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        if (entityList != null && !entityList.isEmpty()) {
            try {
                holidayService.addHoliday(entityList);
            } catch (Exception e) {
                result = super.getFailedRestServiceResult("添加失败！日期已存在");
                ExceptionLogger.error(e);
            }
        }
        return result;
    }

    /**
     * 功能描述:
     *
     * @param ids 删除节假日
     * @return RestServiceResult 删除结果
     * @author YaoS
     * @date 19/2/21 10:42
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public RestServiceResult<Object> deleteHolidayList(@RequestParam String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        if (list != null && !list.isEmpty()) {
            try {
                holidayService.deleteHoliday(list);
            } catch (Exception e) {
                result = super.getFailedRestServiceResult("删除失败！" + e.getMessage());
                ExceptionLogger.error(e);
            }
        }
        return result;
    }

    /**
     * 功能描述: 检验当前是否工作日
     *
     * @param date 当前日期
     * @return -1普通日，0节假日，1调整上班日 是否节假日
     * @author YaoS
     * @date 19/2/21 10:57
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public RestServiceResult<Object> isHoliday(@RequestParam Date date) {
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        try {
            String check = holidayService.isHoliday(date);
            int day = CONVENTIONAL;
            if (HOLI_DAY.equals(check)) {
                day = HOLI_DAY;
            } else if (WORK_DAY.equals(check)) {
                day = WORK_DAY;
            }
            result.setData(day);
        } catch (Exception e) {
            result = super.getFailedRestServiceResult("检查失败！" + e.getMessage());
            ExceptionLogger.error(e);
        }
        return result;
    }

    /**
     * 功能描述: 查询当前年所有的节假日
     *
     * @param page  页数
     * @param limit 条数
     * @param year  查询条件
     * @return RestServiceResult 查询结果集
     * @author YaoS
     * @date 19/2/21 10:59
     */
    @IgnoreAuthority
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public RestServiceResult<Object> selectAllForPage(int page, int limit, @RequestParam String year) {
        Map<String, Object> params = new HashMap<>(2);
        Page pager = super.getPage(page, limit);
        params.put("page", pager);
        params.put("year", year);
        List<HolidayEntity> list = holidayService.selectAllForPage(params);
        return super.getPageSuccessRestServiceResult(pager.getTotalCount(), list);
    }


    @RequestMapping(value = "/findAllFar.json", method = RequestMethod.GET)
    public RestServiceResult<List<Map<String, Object>>> selectAllFar() {
        RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<>();
        try {
            List<Map<String, Object>> list = holidayService.selectAllFar(null);
            r.setData(list);
        } catch (Exception e) {
            r.setMsg("查询所有节日失败！");
        }
        return r;
    }
}
